package homework;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Test04 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        File file = new File("C:\\Users\\Administrator\\IdeaProjects\\JSD2111");//创建根目录文件
        FanXuLie(file);
    }
    private static void FanXuLie(File target) throws IOException, ClassNotFoundException {
        if (target == null) return;  //如果文件为空，则返回
        File[] files = target.listFiles();  //列出所有子文件

        for (File s : files) { //迭代
            if (s.isFile()&&s.getName().endsWith(".obj")) {  // 如果文件的名字有.obj
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(s)); //对象输入流
                Object obj = (Object) ois.readObject(); //用Object类接收反序列化出来的对象
               if(obj instanceof User) {    //如果obj对象属于User，则把obj转换为User
                   User user = (User) obj;
                   System.out.println(user.toString());  //输出信息
               }else {  //否则不知道什么对象，则用超类Object的toString输出
                   System.out.println(obj.toString());
               }

            }
        }
    }
        }

